package com.junjie.web.action.main;

import java.util.List;

import com.junjie.dao.impl.BookDaoImpl;
import com.junjie.dao.impl.CategoryDaoImpl;
import com.junjie.entity.Book;
import com.junjie.entity.Category;
import com.junjie.util.ConfigUtil;
import com.junjie.web.action.BaseAction;

public class MainAction extends BaseAction{
	//------------------------------
	private List<Category> categoryList;//图书列表属性
	private List<Book> bookList;//product.jsp中查询到的图书列表
	private String nowPosition;//当前位置
	private int pageNo;//从第几条开始
	private int pageSize;//每页显示的条数
	private int pageCount;//一共需要多少页
	private String str="";//按条件排序是的条件
	
	private Category category;//图书类别类
	private Book book;
	private String likeName;//模糊书名
	
	private CategoryDaoImpl categoryDao = new CategoryDaoImpl();//后期添加一个Factory工具类
	private BookDaoImpl bookDao = new BookDaoImpl();
	//-------------------------------
	public String execute(){
		
		return "main";
	}
	/**
	 * 弹出广告窗体
	 */
	public String openPage(){
		return "open_page";
	}
	/**
	 * 打开商品显示页面product.jsp
	 * 
	 */
	public String openProduct(){
		
		return "product";
	}
	/**
	 * 加载图书主页 的左侧图书分类导航/
	 */
	public String categoryShow(){
		//查询数据库中的所有数据分类信息并按要求 加载到页面中
		try {
			categoryList = categoryDao.showCategory();
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "category";
	}
	/**
	 * 加载最新添加板块,最新添加的4本小说
	 */
	public String newAddBook(){
		try {
			//小说标签里的书籍
			bookList = bookDao.findNewAddBook();
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "new";
	}
	/**
	 * 加载重点关注块,
	 */
	public String guanZhu(){
		try {
			//编辑推荐图书8条
			bookList = bookDao.findBookByTuiJian();
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "guanZhu";
	}
	/**
	 *连接到product.jsp
	 *1.在左侧浏览同级分类中显示图书分类
	 *2.在"您现在的位置中添加当前的位置"
	 *3.在中间部分显示查询到的点击分类下的所有的图书信息(分页显示)
	 *需要的数据();
	 * @return
	 */
	public String productShow(){
		try {
			//根据传过来的参数category.id和category.parentId
			//获取到对象category并将其保存到session中
			session.put("category", category);
			//获取当前位置
			nowPosition = categoryDao.findCategoryName((Category)session.get("category")).getName();
			//1.显示同级的分类信息,调用dao中的查询方法
			categoryList = categoryDao.findCategoryByParentId((Category)session.get("category"));
			//根据category的Id查询图书,调用bookDao的分页查询方法,默认的是从第一条数据开始,每页显示5条数据
			//1.一共查询到了多少条记录
			int listCount = bookDao.findByCategoryId((Category)session.get("category")).size();
			//2.分页查询到的数据
			bookList = bookDao.findByCategoryId((Category)session.get("category"),str,pageSize*(pageNo-1), pageSize);
			//3.计算总共需要多少页显示
			pageCount = listCount%pageSize==0?listCount/pageSize:(listCount/pageSize)+1;
			//将以后页面跳转需要用到的数据,添加到session中
			session.put("categoryList", categoryList);
			session.put("nowPosition",nowPosition);
			session.put("bookList", bookList);
			session.put("pageCount", pageCount);
			session.put("pageNo", pageNo);
			session.put("pageSize", pageSize);
		}catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return openProduct();
	}
	/**
	 * 按条件排序查询到的图书列表
	 * @return
	 */
	public String orderBookList(){
		category = (Category)session.get("category");
		productShow();
		return openProduct();
	}
	/**
	 * 点击图书名称或者图书图片跳转到图书介绍页面
	 * @return
	 */
	public String bookInfo(){
		try {
			book = bookDao.selectById(book.getId());
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		session.put("book", book);
		return "book_info";
	}
	/**
	 * header.jsp 中的搜索操作,在图书中搜索(按书名进行模糊查询)
	 * @return
	 */
	public String searchBook(){
		System.out.println("searchBook");
		System.out.println(likeName);
		try {
			bookList = bookDao.findBookByLikeName(likeName);
			session.put("bookList", bookList);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "search_book";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	//-------------------------------------------------getter,setter方法-----------
	public List<Category> getCategoryList() {
		return categoryList;
	}
	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public List<Book> getBookList() {
		return bookList;
	}
	public void setBookList(List<Book> bookList) {
		this.bookList = bookList;
	}
	public String getNowPosition() {
		return nowPosition;
	}
	public void setNowPosition(String nowPosition) {
		this.nowPosition = nowPosition;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public String getLikeName() {
		return likeName;
	}
	public void setLikeName(String likeName) {
		this.likeName = likeName;
	}
}
